namespace ManyToManyEntityFramework.Migrations
{
    using ManyToManyEntityFramework.Model;
    using ManyToManyEntityFramework.Models;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<ManyToManyEntityFramework.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ApplicationDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            //Seeds the Students
            context.Students.AddOrUpdate(
                x => x.StudentId,
                new Student() { StudentId = 1, Name = "Jeremy", Grade = Grade.Masters },
                new Student() { StudentId = 2, Name = "Sherry", Grade = Grade.Sophomore },
                new Student() { StudentId = 3, Name = "Eddie", Grade = Grade.Junior },
                new Student() { StudentId = 4, Name = "Sid", Grade = Grade.Graduate },
                new Student() { StudentId = 5, Name = "Ricky", Grade = Grade.Doctorate },
                new Student() { StudentId = 6, Name = "Russell", Grade = Grade.Freshman }
                );

            //Seeds the Courses
            context.Courses.AddOrUpdate(
                c => c.CourseId,
                new Course() { CourseId = 1, Name = "Javascript 101", NumCredits = 5 },
                new Course() { CourseId = 2, Name = "JQuery 101", NumCredits = 4 },
                new Course() { CourseId = 3, Name = "Angular 101", NumCredits = 2 }
                );

            //Saves the new Students and Courses, so we have them for the lookup table
            context.SaveChanges();

            //Gets IDs for all of the Students that we seed
            int JeremyId = context.Students.FirstOrDefault(x => x.Name == "Jeremy").StudentId;
            // ------ Same thing as ------
            // int JeremyId = 1;
            int SherryId = context.Students.FirstOrDefault(x => x.Name == "Sherry").StudentId;
            int EddieId = context.Students.FirstOrDefault(x => x.Name == "Eddie").StudentId;
            int SidId = context.Students.FirstOrDefault(x => x.Name == "Sid").StudentId;
            int RickyId = context.Students.FirstOrDefault(x => x.Name == "Ricky").StudentId;
            int RussellId = context.Students.FirstOrDefault(x => x.Name == "Russell").StudentId;
            //Gets IDs for all of the Courses that we seed
            int JSId = context.Courses.FirstOrDefault(x => x.Name == "Javascript 101").CourseId;
            int JQId = context.Courses.FirstOrDefault(x => x.Name == "JQuery 101").CourseId;
            int NGId = context.Courses.FirstOrDefault(x => x.Name == "Angular 101").CourseId;

            //Seeds the Lookup Table
            context.Students_Courses.AddOrUpdate(
                x => x.Students_CoursesId,
                //Jeremy
                new Students_Courses() { Students_CoursesId = 1, StudentId = JeremyId, CourseId = NGId },
                new Students_Courses() { Students_CoursesId = 2, StudentId = JeremyId, CourseId = JSId },
                new Students_Courses() { Students_CoursesId = 3, StudentId = JeremyId, CourseId = JQId },
                //Sherry
                new Students_Courses() { Students_CoursesId = 4, StudentId = SherryId, CourseId = JQId },
                new Students_Courses() { Students_CoursesId = 5, StudentId = SherryId, CourseId = NGId },
                //Eddie
                new Students_Courses() { Students_CoursesId = 6, StudentId = EddieId, CourseId = JSId },
                new Students_Courses() { Students_CoursesId = 7, StudentId = EddieId, CourseId = JQId },
                //Sid
                new Students_Courses() { Students_CoursesId = 8, StudentId = SidId, CourseId = JSId },
                new Students_Courses() { Students_CoursesId = 9, StudentId = SidId, CourseId = NGId },
                //Ricky
                new Students_Courses() { Students_CoursesId = 10, StudentId = RickyId, CourseId = JSId },
                //Russell
                new Students_Courses() { Students_CoursesId = 11, StudentId = RussellId, CourseId = JQId }
                );
            context.SaveChanges();
        }
    }
}